<template>
  <div
    style="
      width: 100%;
      height: 100vh;
      background-color: darkslateblue;
      overflow: hidden;
    "
  >
    <div style="width: 400px; margin: 150px auto">
      <div
        style="
          color: #cccccc;
          font-size: 30px;
          text-align: center;
          padding: 30px 0;
        "
      >
        欢迎登录
      </div>
      <el-form ref="form" :model="form" size="normal" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="form.username">
            <template #prefix>
              <el-icon class="el-input__icon"><UserFilled /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" show-password>
            <template #prefix>
              <el-icon class="el-input__icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" @click="login"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { UserFilled, Lock } from "@element-plus/icons-vue";
import request from "@/utils/request";
export default {
  name: "Login",
  components: {
    UserFilled,
    Lock,
  },
  data() {
    return {
      form: {},
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    login() {
      // 如果合法，发送请求
      this.$refs["form"].validate((valid) => {
        if (valid) {
          request.post("/consumer/login", this.form).then((response) => {
            if (response.result === "SUCCESS") {
              this.$message({
                type: "success",
                message: "登录成功",
              });
              // 登录成功 页面跳转到主页
              this.$router.push("/");
            } else {
              this.$message({
                type: "error",
                message: response.message,
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style></style>
